$(document).ready(function () {
	var multiple_action_ids;

	function update_multiple_action_ids () {
		multiple_action_ids = '';

		$('td.actions :checkbox').each(function () {
			if ($(this).is(':checked')) {
				multiple_action_ids += $(this).attr('name') + ',';
				$(this).parents('tr').addClass('select');
			} else {
				$(this).parents('tr').removeClass('select');
			}
		});

		if (multiple_action_ids) {
			$('div.actions a.button').show();
		} else {
			$('div.actions a.button').hide();
		}
	}

	update_multiple_action_ids();

	$('td.actions :checkbox').click(update_multiple_action_ids);

	$('#option-select-all').click(function () {
		$('td.actions :checkbox').attr('checked', 'checked');

		update_multiple_action_ids();

		return false;
	});

	$('#option-select-none').click(function () {
		$('td.actions :checkbox').attr('checked', '');

		update_multiple_action_ids();

		return false;
	});

	$('#option-invert-selection').click(function () {
		$('td.actions :checkbox').each(function () {
			if ($(this).is(':checked')) {
				$(this).attr('checked', '');
			} else {
				$(this).attr('checked', 'checked');
			}
		});

		update_multiple_action_ids();

		return false;
	});

	$('div.actions a.button').click(function () {
		var href = $(this).attr('href');
		href = href.replace('|id|', multiple_action_ids);
		$(this).attr('href', href);

		return true;
	});
});